import axios from 'axios';
import store from '@/store';
import router from "../router";
import {ElMessage} from "element-plus";
import {endLoad, getToken, removeToken} from "./loginFunctions";

const requerts = axios.create({
    // 基础路径
    baseURL:'http://101.43.182.17:2323/',
    // baseURL:'http://50k688j777.zicp.fun/',
    //baseURL:'http://192.168.43.210:2323/',
    //baseURL:'http://192.168.1.104:2323/',
    // baseURL:'http://127.0.0.1:7777/',
    timeout:3000
});
// 请求拦截器:在发请求之前请求拦截器可以检测到,在请求发出去之前做一些事情
requerts.interceptors.request.use((config) => {
    let token = getToken();
    if(token){
        config.headers.common['Authorization'] = token;
    }
    return config;
}, error =>{
    console.log(error);
    //对请求错误做什么
    return Promise.reject(error);
});
// 响应拦截器
requerts.interceptors.response.use((response) => {
    endLoad();
    console.log(response.data);
    switch (response.data.code){
        //421没token
        case 421:
            router.replace({path: '/'});
            return;
        //token过期
        case 422:
            removeToken();
            router.replace({path: '/'});
            return;
        // default:
        //     return Promise.reject(new Error('faile'));
    }
    return response.data;
},(error) => {
    endLoad();
    ElMessage({showClose: true, message: '请检查网络!', type: 'warning', grouping: true})
    //设置加载状态
    // store.commit("SetLoading",false);
    console.log(error,error.response,"=============");
    switch (error.response.status){
        //421没token
        case 421:
            router.replace({path: '/'});
            return;
        //token过期
        case 422:
            removeToken();
            router.replace({path: '/'});
            return;
        // default:
        //     return Promise.reject(new Error('faile'));
    }
});
// 对外暴露
export default requerts;
